#include "stdafx.h"
#include "Subtraction.h"	


bool Subtraction::Run( std::string& tape )
{
	std::string pop( tape.substr( GetHead(), 1 ) );


	switch( GetState() )
	{
	case State::_1 :
		{
			if( !pop.empty() )
			{
				MoveRight();
				SetState( State::_1 );
			}
			else
			{
				MoveLeft();
				SetState( State::_2 );
			}
		}
		break;

	case State::_2 :
		{
			if( ZERO == pop )
			{
				Delete( tape, GetHead() );
				MoveLeft();
				SetState( State::_3 );
			}
			else if( ONE == pop )
			{
				Delete( tape, GetHead() );
				MoveLeft();
				SetState( State::_4 );
			}
			else if( MINUS == pop )
			{
				Delete( tape, GetHead() );
				MoveLeft();
				SetState( State::_8 );
			}
		}
		break;

	case State::_3 :
		{
			if( MINUS == pop )
			{
				MoveLeft();
				SetState( State::_5 );
			}
			else
			{
				MoveLeft();
				SetState( State::_3 );
			}
		}
		break;

	case State::_4 :
		{
			if( MINUS == pop )
			{
				MoveLeft();
				SetState( State::_6 );
			}
			else
			{
				MoveLeft();
				SetState( State::_4 );
			}
		}
		break;

	case State::_5 :
		{
			if( ZERO == pop )
			{
				Delete( tape, GetHead() );
				New( tape, BLANK, GetHead() );
				MoveRight();
				SetState( State::_1 );
			}
			else if( ONE == pop )
			{
				Delete( tape, GetHead() );
				New( tape, DUMMY, GetHead() );
				MoveRight();
				SetState( State::_1 );
			}
			else// if( BLANK == pop || DUMMY == pop )
			{
				MoveLeft();
				SetState( State::_5 );
			}
		}
		break;

	case State::_6 :
		{
			if( ZERO == pop )
			{
				Delete( tape, GetHead() );
				New( tape, DUMMY, GetHead() );
				MoveLeft();
				SetState( State::_7 );
			}
			else if( ONE == pop )
			{
				Delete( tape, GetHead() );
				New( tape, BLANK, GetHead() );
				MoveRight();
				SetState( State::_1 );
			}
			else// if( BLANK == pop || DUMMY == pop )
			{
				MoveLeft();
				SetState( State::_6 );
			}
		}
		break;

	case State::_7 :
		{
			if( ZERO == pop )
			{
				Delete( tape, GetHead() );
				New( tape, ONE, GetHead() );
				MoveLeft();
				SetState( State::_7 );
			}
			else if( ONE == pop )
			{
				Delete( tape, GetHead() );
				New( tape, ZERO, GetHead() );
				MoveRight();
				SetState( State::_1 );
			}
			else if( pop.empty() )
			{
				Delete( tape, GetHead() );
				New( tape, MINUS, GetHead() );
				MoveRight();
				SetState( State::_1 );
			}
		}
		break;

	case State::_8 :
		{
			if( BLANK == pop )
			{
				Delete( tape, GetHead() );
				New( tape, ZERO, GetHead() );
				MoveLeft();
				SetState( State::_8 );
			}
			else if( DUMMY == pop )
			{
				Delete( tape, GetHead() );
				New( tape, ONE, GetHead() );
				MoveLeft();
				SetState( State::_8 );
			}
			else if( 0 == GetHead() )
			{				
				SetState( State::Halt );				
			}
			else
			{
				MoveLeft();
				SetState( State::_8 );
			}
		}
		break;
	}

	SetStep( GetStep() + 1 );


	return ( State::Halt == GetState() );
}